package com.ruyuan.eshop.construction.api.impl;

import com.alibaba.fastjson.JSON;
import com.ruyuan.eshop.common.core.JsonResult;
import com.ruyuan.eshop.construction.api.FrontCategoryApi;
import com.ruyuan.eshop.construction.domain.dto.FrontCategoryDTO;
import com.ruyuan.eshop.construction.domain.dto.FrontCategoryTreeDTO;
import com.ruyuan.eshop.construction.domain.dto.FrontCategoryResultDTO;
import com.ruyuan.eshop.construction.domain.request.QueryCategoryRequest;
import com.ruyuan.eshop.construction.domain.request.FrontCategoryRequest;
import com.ruyuan.eshop.common.exception.ProductBizException;
import com.ruyuan.eshop.construction.service.FrontCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 类目服务
 *
 * @author zhonghuashishan
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = FrontCategoryApi.class, retries = 0)
public class FrontCategoryApiImpl implements FrontCategoryApi {

    @Autowired
    private FrontCategoryService frontCategoryService;


    @Override
    public JsonResult<FrontCategoryResultDTO> frontCategory(FrontCategoryRequest request) {
        try {
            FrontCategoryResultDTO frontCategoryResultDTO =
                    frontCategoryService.saveFrontCategory(request);
            return JsonResult.buildSuccess(frontCategoryResultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<List<FrontCategoryTreeDTO>> frontCategoryTree(QueryCategoryRequest request) {
        try {
            List<FrontCategoryTreeDTO> frontCategoryTreeDTOS = frontCategoryService.selectTree(request);
            return JsonResult.buildSuccess(frontCategoryTreeDTOS);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<List<FrontCategoryDTO>> frontCategoryChild(QueryCategoryRequest request) {
        try {
            List<FrontCategoryDTO> frontCategoryDTOS = frontCategoryService.selectChild(request);
            return JsonResult.buildSuccess(frontCategoryDTOS);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

}
